package com.example.commentserver.service;

import com.example.commentserver.entity.Comment;
import com.example.commentserver.manager.CommentManager;
import com.example.commentserver.params.CommentParams;
import com.example.commentserver.params.PageCommentParams;
import com.example.commentserver.vo.CommentVO;
import com.example.userserver.enums.ReturnEnum;
import com.example.userserver.utils.BeanCopyUtils;
import com.example.userserver.vo.Result;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CommentService {
    @Autowired
    private CommentManager commentManager;
    public PageInfo<CommentVO> pageComment(PageCommentParams pageCommentParams){
        PageInfo<CommentVO> pageInfo = PageHelper.startPage(pageCommentParams)
                .doSelectPageInfo(() ->  commentManager.listComment(pageCommentParams));
        return pageInfo;
    }

    public void deleteComment(String id) {
        commentManager.deleteComment(id);
    }

    public Result updateComment(CommentParams commentParams) {
        Comment comment = new Comment();
        BeanCopyUtils.copyPropertiesIgnoreNull(commentParams, comment);
        commentManager.updateComment(comment);
        return new Result(null, ReturnEnum.SUCCESS);
    }

    public Result insertComment(CommentParams commentParams) {
        Comment comment = new Comment();
        BeanCopyUtils.copyPropertiesIgnoreNull(commentParams, comment);
        commentManager.insertComment(comment);
        return new Result(null, ReturnEnum.SUCCESS);
    }
}
